﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ilibrary.source.model.metasearch
{
    
    /// <summary>
    ///  A helper class for building REST URLs for 
    /// Amazon E-Commerce Service (ECS) operations
    /// </summary>
    public class ECSHelper
    {

        static ECSHelper()
        {
            //populate locales
            _baseURL.Add("US", prefix + "com" + suffix);
            _baseURL.Add("UK", prefix + "co.uk" + suffix);
            _baseURL.Add("DE", prefix + "de" + suffix);
            _baseURL.Add("JP", prefix + "co.jp" + suffix);
            _baseURL.Add("CA", prefix + "ca" + suffix);
            _baseURL.Add("FR", prefix + "fr" + suffix);
        }

        public ECSHelper(String locale)
        {
            _locale = locale.ToUpper();

        }

        public void clear()
        {
            _properties.Clear();
        }

        public String getLocale()
        {
            return _locale;
        }

        public String getProperty(String name)
        {
            return (String)_properties[name];
        }

        public void setLocale(String locale)
        {
            _locale = locale.ToUpper();
        }

        public void setProperty(String name, String value)
        {
            if (value == null || value.Length == 0)
            {
                _properties.Remove(name);
            }
            else
            {
                _properties.Add(name, value);
            }
        }

        public String toString()
        {
            StringBuilder b = new StringBuilder();



            b.Append((String)_baseURL[_locale]);
            b.Append("&SubscriptionId=");
            b.Append(_subID);

            if (_assocTag != null)
            {
                b.Append("&AssociateTag=");
                b.Append(_assocTag);
            }

            foreach (String s in _properties.Keys)
            {
                String name = (String)s;
                String value = (String)_properties[name];



                b.Append('&');
                b.Append(name);
                b.Append('=');
                b.Append(value);
            }

            return b.ToString();
        }

        private String _assocTag = "httpwwwycsoft-20";
        private String _locale;
        private Dictionary<String, String> _properties = new Dictionary<String, String>();
        private String _subID = "0Q3BPPFRAD7FSNWW0CG2";

        // Build the table of base URLs

        private static Dictionary<String, String> _baseURL = new Dictionary<String, String>();


        private static String prefix = "http://webservices.amazon.";
        private static String suffix = "/onca/xml?Service=AWSECommerceService";

    }
}
